package com.csproj.siiat.base.common.service;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;

import com.csproj.siiat.base.common.util.TLMessage.TLMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.csproj.siiat.config.ApplicationProperties;
import com.csproj.siiat.base.common.util.TCPServer;

import static com.csproj.siiat.base.common.service.TCPReceiverService.MQ;
import static org.apache.commons.configuration.PropertyConverter.toByte;

/**
 * Created by tyshi on 2018/9/26.
 * 
 * this will be main thread to receiving TCP heartbeat from remote
 * then validate and parse the received digits into queque
 * 
 */

@Service
public class TCPReceiverTestService {

    private final Logger logger = LoggerFactory.getLogger(TLMessage.class);

    private ApplicationProperties applicationProperties;
    
    private static int interval = 0;

    private List<OutputStream> outputStreams= new ArrayList<OutputStream>();

    public TCPReceiverTestService(ApplicationProperties applicationProperties) {
    	this.applicationProperties = applicationProperties;
    }
    @Scheduled(fixedRate = 30 * 1000)
    public void send() {

    	if (!applicationProperties.getTcpReceiverTestServiceEnable()) {
    		//do nothing
    		return;
    	}
        if (outputStreams.size() == 0) {
            for (int i = 0; i< applicationProperties.getTcpClientNumber(); i++) {
                try {
                    Socket socket=new Socket("localhost",applicationProperties.getTcpReceiverPort());
                    OutputStream outputStream= null;
                    outputStream = socket.getOutputStream();
                    outputStreams.add(outputStream);
                } catch (IOException e) {
                    logger.error("",e);
                }

            }

        }

        for (OutputStream os: outputStreams) {
            byte[] b = hexStringToByte("AA37C401607B0080901F013F090114340F350F2C003F002B4B003E07000703041A0F1D1200000000000000005A10010F01070101010622");
            //byte[] b = hexStringToByte("AA21C500D3A29800980000000000000000000EDC0000000000120B09082A3205A9");
            try {
                os.write(b);
                logger.debug("sent bytes");
            } catch (IOException e) {
                logger.error("",e);
            }
        }


    }
    public  byte[] hexStringToByte(String hex) {
        int len = (hex.length() / 2);
        logger.info("length is " + len);
        byte[] result = new byte[len];
        char[] achar = hex.toCharArray();
        for (int i = 0; i < len; i++) {
            int pos = i * 2;
            result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
        }
        return result;
    }

    private static byte toByte(char c) {
        byte b = (byte) "0123456789ABCDEF".indexOf(c);
        return b;
    }
}
